<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE sqlMap PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN" "http://ibatis.apache.org/dtd/sql-map-2.dtd" >
<sqlMap namespace="com.uc.training.smadmin.gds.dao.PropertyDao">
    <!--新增商品属性-->
    <insert id="insertProperty" parameterClass="com.uc.training.smadmin.gds.model.Property">
        INSERT INTO t_gds_property
        (goods_id, stock, discount_price, sale_price, property, is_discount, create_emp, modify_emp)
        VALUES (#goodsId#,#stock#,#discountPrice#,#salePrice#,#property#,#isDiscount#,#createEmp#,#modifyEmp#)
        <selectKey resultClass="java.lang.Long" keyProperty="id">
            SELECT LAST_INSERT_ID() as id;
        </selectKey>
    </insert>

    <!--通过商品id获取商品属性数量-->
    <select id="getPropertyIdListByGoodsId" parameterClass="Long" resultClass="Long">
        select id
        from t_gds_property
        where goods_id = #goodsId#
    </select>

    <!--根据id获取属性-->
    <select id="getPropertyById" parameterClass="Long" resultClass="com.uc.training.smadmin.gds.re.PropertyRE">
        select
          id,
          goods_id AS goodsId,
          stock,
          discount_price AS discountPrice,
          sale_price AS salePrice,
          property,
          is_discount AS isDiscount
        from t_gds_property
        where id = #id#
    </select>

    <!--通过商品id获取商品属性-->
    <select id="getPropertyListByGoodsId" parameterClass="Long" resultClass="com.uc.training.smadmin.gds.re.AdminPropertyListRE">
        select
          id,
          goods_id AS goodsId,
          stock,
          discount_price AS discountPrice,
          sale_price AS salePrice,
          property,
          is_discount AS isDiscount
        from t_gds_property
        where goods_id = #goodsId#
    </select>
    <!--通过主键id删除商品属性-->
    <delete id="deletePropertyById" parameterClass="Long">
        delete from t_gds_property
        where id = #id#
    </delete>

    <!--获取商品该名称规格的数量-->
    <select id="getCountByProperty" parameterClass="com.uc.training.smadmin.gds.model.Property" resultClass="Integer">
      select count(1)
      from t_gds_property
      where 1=1
        <isNotEmpty property="property" prepend="and">
            `property` = #property#
        </isNotEmpty>
        <isNotEmpty property="goodsId" prepend="and">
            `goods_id` = #goodsId#
        </isNotEmpty>
    </select>

    <!--更新商品属性-->
    <update id="updateProperty" parameterClass="com.uc.training.smadmin.gds.model.Property">
        UPDATE t_gds_property
        SET   stock=#stock#,
              discount_price=#discountPrice#,
              sale_price=#salePrice#,
              property=#property#,
              is_discount=#isDiscount#,
              modify_emp=#modifyEmp#
        WHERE id = #id#
    </update>
</sqlMap>